package com.tc.itfarm.web.action;

import com.tc.itfarm.api.common.JsonMessage;
import com.tc.itfarm.api.util.BeanMapper;
import com.tc.itfarm.api.util.EncoderUtil;
import com.tc.itfarm.model.User;
import com.tc.itfarm.service.FavoriteService;
import com.tc.itfarm.service.UserService;
import com.tc.itfarm.web.vo.UserVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by wangdongdong on 2016/8/30.
 */
@Controller
@RequestMapping("/login")
public class LoginAction {

    @Resource
    private UserService userService;
    @Resource
    private FavoriteService favoriteService;

    @RequestMapping("toLogin")
    public String toLogin(HttpServletRequest request) {
        return "login/login";
    }

    /**
     * 登录
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("/loginDo")
    @ResponseBody
    public JsonMessage loginDo(User user, HttpServletRequest request) {
        User u = userService.select(user.getUsername(), EncoderUtil.md5(user.getPassword()));
        JsonMessage jm = new JsonMessage();
        if (u != null) {
            jm.setResultMessage("登陆成功!");
            jm.setSuccess(true);
            UserVO userVO = new UserVO();
            jm.setResult(userVO);
            BeanMapper.copy(userVO, u);
            userVO.setFavoriteIds(favoriteService.selectArticleIds(u.getRecordId()));
            request.getSession().setAttribute("user", userVO);
        } else {
            jm.setResultMessage("用户名或密码错误!");
            jm.setSuccess(false);
        }
        return jm;
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @RequestMapping("loginOut")
    @ResponseBody
    public JsonMessage loginOut(HttpServletRequest request) {
        request.getSession().invalidate();
        JsonMessage jm = new JsonMessage();
        jm.setResultMessage("注销成功!");
        jm.setSuccess(true);
        return jm;
    }

    /**
     * 注册
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public JsonMessage register(User user, HttpServletRequest request) {
        if (user.getUsername() == null || user.getUsername().trim().equals("")) {
            return new JsonMessage(JsonMessage.STATUS_FAIL,"用户名不能为空!");
        }
        if (user.getNickname() == null || user.getNickname().trim().equals("")) {
            return new JsonMessage(JsonMessage.STATUS_FAIL,"昵称不能为空!");
        }
        if (user.getTelephone() == null || user.getTelephone().trim().equals("")) {
            return new JsonMessage(JsonMessage.STATUS_FAIL,"手机号不能为空!");
        }
        if (user.getPassword() == null || user.getPassword().trim().equals("")) {
            return new JsonMessage(JsonMessage.STATUS_FAIL,"密码不能为空!");
        }
        if (userService.selectByUsername(user.getUsername()) != null) {
            return new JsonMessage(JsonMessage.STATUS_FAIL,"该用户名已被注册!");
        }
        user.setStatus(1);
        Integer result = userService.save(user);
        return new JsonMessage(JsonMessage.STATUS_SUCCESS,"注册成功!");
    }
}
